package com.seeyou.lake.ui.edt

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.seeyou.lake.R
import com.seeyou.lake.base.MyBaseActivity
import com.seeyou.lake.base.EventBusVo
import com.seeyou.lake.bean.ModelBean
import com.seeyou.lake.databinding.ActivityPreviewPageBinding
import com.seeyou.lake.util.BitmapUtils
import com.seeyou.lake.util.GlideUtil
import com.seeyou.lake.viewmodel.ModifyViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class PreviewPageActivityMy : MyBaseActivity<ActivityPreviewPageBinding, ModifyViewModel>() {

    private var mContent: String = ""
    private val dataList: MutableList<ModelBean> = arrayListOf()
    private var mBitmap: Bitmap? = null

    companion object {
        fun open(activity: Activity, content: String) {
            val intent = Intent(activity, PreviewPageActivityMy::class.java)
            intent.putExtra("content", content)
            activity.startActivity(intent)
        }
    }

    override fun myViewModel(): ModifyViewModel {
        return ModifyViewModel()
    }

    override fun yourViewBing(): ActivityPreviewPageBinding {
        return ActivityPreviewPageBinding.inflate(layoutInflater)
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        mViewBinding.ivPreviewHeader.setOnClickListener { finish() }
        mViewBinding.tvPreviewNext.setOnClickListener {
            if (mBitmap == null) {
                return@setOnClickListener
            }
            if (mContent.isEmpty()) {
                return@setOnClickListener
            }
            val file = BitmapUtils.bitmapToFile(applicationContext, mBitmap)
            TitleInputActivityMy.open(this, file.absolutePath, mContent)
        }
        mContent = intent.getStringExtra("content") ?: ""


        val linearLayoutManager =
            object : LinearLayoutManager(this, RecyclerView.HORIZONTAL, false) {
                override fun canScrollHorizontally(): Boolean {
                    return true
                }

                override fun canScrollVertically(): Boolean {
                    return false
                }
            }


        val adapter = object : BaseQuickAdapter<ModelBean, QuickViewHolder>() {
            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): QuickViewHolder {
                // 返回一个 ViewHolder
                return QuickViewHolder(R.layout.item_preview_select, parent)
            }

            override fun onBindViewHolder(
                holder: QuickViewHolder,
                position: Int,
                item: ModelBean?
            ) {
                holder.setIsRecyclable(false)
                val strokeView = holder.getView<View>(R.id.v_item_stroke)
                val imgView = holder.getView<ImageView>(R.id.iv_item_model)
                GlideUtil.loadRoundImage(context, item!!.url, 10, imgView)
                if (item.selected) {
                    strokeView.visibility = View.VISIBLE
                } else {
                    strokeView.visibility = View.GONE
                }
                imgView.setOnClickListener {
                    item.selected = !item.selected
                    strokeView.visibility = View.VISIBLE
                    if (item.selected) {
                        dataList.forEachIndexed { index, modelBean ->
                            if (position != index) {
                                modelBean.selected = false
                            }
                        }
                        mViewBinding.viewLoading.visibility = View.VISIBLE
                        mVModel.toImg(mContent,item.url,item.type)
                    } else {
                        strokeView.visibility = View.GONE
                    }
                    notifyDataSetChanged()
                }
            }
        }

        mViewBinding.rvPreviewSelect.layoutManager = linearLayoutManager
        mViewBinding.rvPreviewSelect.adapter = adapter

        mVModel.toImg(mContent, "https://www.godboy.cn/download/poetry/d1.png", 1)
        mVModel.getModelList()
        mViewBinding.viewLoading.visibility = View.VISIBLE


        mVModel.imgStreamResponse.observe(this) {
            runOnUiThread {
                mBitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
                mViewBinding.ivPreview.setImageBitmap(mBitmap)
                mViewBinding.viewLoading.visibility = View.GONE
            }
        }

        mVModel.modelListResponse.observe(this) {
            dataList.clear()
            dataList.addAll(it)
            adapter.submitList(dataList)
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun finishEvent(event: EventBusVo?) {
        if (event == null) {
            return
        }

        if (event.type == 1) {
            finish()
        }
    }

}